package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.membershipcard;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.base.BaseApplation;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.util.CusCountDownUtils;
import com.zzsy.carosshopprojects.util.StringUtils;
import com.zzsy.carosshopprojects.util.ThrottleFirstUtil;
import com.zzsy.carosshopprojects.util.ViewBgUtil;

import butterknife.BindView;
import butterknife.OnClick;

import static android.content.Context.INPUT_METHOD_SERVICE;

/**
 * 会员补卡
 */
public class CardReplaceFragment extends BaseFragment {

    public static CardReplaceFragment newInstance(String phone, String kind) {
        CardReplaceFragment cardReplaceFragment = new CardReplaceFragment();
        Bundle bundle = new Bundle();
        bundle.putString("phone", phone);
        bundle.putString("kind", kind);
        cardReplaceFragment.setArguments(bundle);
        return cardReplaceFragment;
    }

    @BindView(R.id.et_phone)
    EditText tvPhone;
    @BindView(R.id.et_user_code)
    EditText etPhoneCode;
    @BindView(R.id.tv_send_sms)
    TextView tvSendMsg;
    @BindView(R.id.ly_main)
    LinearLayout ly_main;
    private String phoneNumStr;
    private CusCountDownUtils cusCountDownUtils;
    private CardReplaceMode cardReplaceMode;
    private String msgId = "";

    @Override
    protected int initLayout() {
        return R.layout.fg_cardreplace;
    }

    @Override
    protected void initView(View view) {
        ly_main.setBackground(ViewBgUtil.setBg(15, Color.parseColor(ConfigureFile.ColorWhite)));
        tvSendMsg.setBackground(ViewBgUtil.setBg(15, Color.parseColor("#FF6900")));
        if (getArguments().getString("kind").equals("1"))
            initTitle(view, getResources().getString(R.string.MemberReplaceCard), getActivity());
        else
            initTitle(view, "验证身份", getActivity());
        cusCountDownUtils = new CusCountDownUtils(getContext(), 60);
        cusCountDownUtils.setOnclickTimeListener(new CusCountDownUtils.OnclickTimeListener() {
            @Override
            public void onTicks(long millisUntilFinished) {
                tvSendMsg.setText(String.format(getString(R.string.count_down), millisUntilFinished / 1000));
                tvSendMsg.setBackground(BaseApplation.instance().getResources().getDrawable(R.drawable.get_code_e5));
                tvSendMsg.setTextColor(BaseApplation.instance().getResources().getColor(R.color.select_text_code));
                tvSendMsg.setClickable(false);
            }

            @Override
            public void onFinish() {
                tvSendMsg.setText("重新获取");
                tvSendMsg.setBackground(BaseApplation.instance().getResources().getDrawable(R.drawable.btn_jb));
                tvSendMsg.setTextColor(BaseApplation.instance().getResources().getColor(R.color.white));
                tvSendMsg.setClickable(true);
            }
        });
    }

    @Override
    protected void initData(Context mContext) {
        cardReplaceMode = getViewModelProvider().get(CardReplaceMode.class);
        tvPhone.setText(getArguments().getString("phone"));
    }

    @Override
    protected void loadMethod() {
        cardReplaceMode.getLiveData().observe(this, msgId -> {
            this.msgId = msgId;
        });
        cardReplaceMode.getliveDataList().observe(this, baseBean -> {
            if (baseBean != null) {
                if (baseBean.getMyMainCards() != null && baseBean.getMyMainCards().size() > 0) {
                    startWithPop(CardSelectFragment.newInstance(phoneNumStr, getArguments().getString("kind")));
                } else {
                    ToastUtils.showToast(getContext(), "该手机号不是会员！");
                    return;
                }
            }
        });
    }

    @OnClick({R.id.tv_loss, R.id.tv_send_sms})
    public void onViewClicked(View view) {
        if (ThrottleFirstUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_loss:
                phoneNumStr = String.valueOf(tvPhone.getText());
                String code = String.valueOf(etPhoneCode.getText());
                if (StringUtils.isEmpty(code)) {
                    ToastUtils.showToast(getContext(), "请输入验证码！");
                    return;
                }
                cardReplaceMode.CheckCode(getContext(), phoneNumStr, msgId, code);
                hideInput();
                break;
            case R.id.tv_send_sms:
                phoneNumStr = String.valueOf(tvPhone.getText());
              //  EditTextUtils.showSoftInputFromWindow(getActivity(), tvPhone);
                if (StringUtils.isEmpty(phoneNumStr)) {
                    ToastUtils.showToast(getContext(), "电话号码不能为空");
                    return;
                }
                if (phoneNumStr.length() != 11) {
                    ToastUtils.showToast(getContext(), "电话号码格式有误");
                    return;
                }
                cusCountDownUtils.start();
                cardReplaceMode.getCode(getContext(), phoneNumStr);
                break;

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        cusCountDownUtils.cancel();
    }

    /**
     * 关闭软键盘
     */
    protected void hideInput() {
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(INPUT_METHOD_SERVICE);
        View v = getActivity().getWindow().peekDecorView();
        if (null != v) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }
}
